#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <vector>
#include <string>
#include <string.h>
#include <fstream>
#include <iostream>
#include "utility/fileio.h"
#include "include/runcode.h"
#include "utility/os.h"
#include "utility/ojData.h"
#include "utility/configure.h"
#include "utility/fileio.h"
#include "utility/log.h"
#include "utility/defer.h"
#include "include/judge.h"
#include "utility/options.h"
using namespace std;

#ifdef __cplusplus
extern "C"
{
#endif

const char* languages[] = {
    "c",
    "c++",
    "python3",
    "lua",
    "go",
    "csharp",
    "php",
    "java",
};
const char* testfiles[] = {
    "user.c",
    "user.cpp",
    "user.py",
    "user.lua",
    "user.go",
    "user.cs",
    "user.php",
    "user.java",
};
const int testCount = sizeof(languages) / sizeof(char*);

int SetOutputFile(char *file)
{
    return Log::instance().setOutputFile(file, std::ios::out | std::ios::trunc);
}

int SetErrorFile(char *file)
{
    return Log::instance().setErrorFile(file, std::ios::out | std::ios::trunc);
}

char *RunJudge(const char *language,
                int judgeType,
                const char *filename,
                const char *initPath,
                const char *dataPath,
                const char *spjPath,
                int timeLimit,
                int memoryLimit,
                int compareFlag,
                int ruler,
                int querySet,
                const char *eps,
                const char *userCachePath,
                const char *spjCachePath
            ){
    ProcessStatus result = runJudge(  language, 
                            judgeType, 
                            filename, 
                            initPath,
                            dataPath, 
                            spjPath,
                            timeLimit, 
                            memoryLimit, 
                            compareFlag, 
                            ruler, 
                            querySet,
                            eps,
                            userCachePath,
                            spjCachePath);
    std::string result_json = result.json();
    int len = result_json.size();
    char *c = (char*)malloc(len + 1);
    strcpy(c, result_json.c_str());
    c[len] = '\0';
    return c;
}
#ifdef ENABLE_DB_JUDGE
#include "judge_types/database_judge.h"
int InitDatabaseParamsFromFile(const char* file) {
    return databaseParams.init_from_file(file);
}
int InitDatabaseParams( const char* host,
                        int port,
                        const char* rootUser,
                        const char* rootPasswd,
                        const char* visitorUser,
                        const char* visitorPasswd) {
        databaseParams.init(host, port, rootUser, rootPasswd, visitorUser, visitorPasswd);
        return 1;
    }
#endif
#ifdef __cplusplus
}
#endif